package cz.hanis.android.geography.adapters;

import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;

import com.actionbarsherlock.app.SherlockActivity;

import cz.hanis.android.geography.GeographyApplication;
import cz.hanis.android.geography.R;
import cz.hanis.android.geography.WrongResourceException;
import cz.hanis.android.geography.models.Country;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class CountryDetailPageAdapter extends PagerAdapter {

	private static final String TAG = "CountryDetailPageAdapter";

	private List<Country> mCountries;
	private SherlockActivity mActivity;

	public CountryDetailPageAdapter(List<Country> countries, SherlockActivity activity) {
		this.mCountries = countries;
		this.mActivity = activity;
	}

	@Override
	public int getCount() {
		return mCountries.size();		
	}

	public Object instantiateItem(View collection, int position) {

		LayoutInflater inflater = (LayoutInflater) collection.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View view = inflater.inflate(R.layout.country_detail_view, null);
		((ViewPager) collection).addView(view, 0);
		applyCountry(position);
		return view;
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView((View) arg2);

	}
	
    @Override
    public CharSequence getPageTitle (int position) {
        return mCountries.get(position).getName();
    }	

	@Override
	public void finishUpdate(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == ((View) arg1);

	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public Parcelable saveState() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
		// TODO Auto-generated method stub

	}

	private void applyCountry(int possition) {
		TextView tvName = (TextView) mActivity
				.findViewById(R.id.tv_country_detail_name);
		TextView tvCapital = (TextView) mActivity
				.findViewById(R.id.tv_country_detail_capital);
		TextView tvContinent = (TextView) mActivity
				.findViewById(R.id.tv_country_detail_continent);
//		TextView tvRegion = (TextView) activity
//				.findViewById(R.id.tv_country_detail_region);		
//		TextView tvLanguages = (TextView) activity
//				.findViewById(R.id.tv_country_detail_languages);
		TextView tvContiguous = (TextView) mActivity
				.findViewById(R.id.tv_country_detail_contiguous);
		TextView tvArea = (TextView) mActivity
				.findViewById(R.id.tv_country_detail_area);
		TextView tvPopulationTotal = (TextView) mActivity
				.findViewById(R.id.tv_country_detail_population_total);
		TextView tvPopulationDensity = (TextView) mActivity
				.findViewById(R.id.tv_country_detail_population_density);
//		TextView tvGovernment = (TextView) activity
//				.findViewById(R.id.tv_country_detail_government);
//		TextView tvCurrency = (TextView) activity
//				.findViewById(R.id.tv_country_detail_currency);
//		TextView tvIso = (TextView) activity
//				.findViewById(R.id.tv_country_detail_iso);
//		TextView tvTld = (TextView) activity
//				.findViewById(R.id.tv_country_detail_tld);									
		ImageView ivFlag = (ImageView) mActivity
				.findViewById(R.id.iv_country_detail_flag);
//		ImageView ivLoc = (ImageView) activity
//				.findViewById(R.id.iv_country_detail_loc);	
//		TextView btWikiLink = (TextView) mActivity
//				.findViewById(R.id.bt_country_detail_link_wiki);
//		


		final Country country = mCountries.get(possition);
		
//		btWikiLink.setOnClickListener(new View.OnClickListener() {			
//			@Override
//			public void onClick(View v) {
//				Intent intent = new Intent(mActivity, WebViewActivity.class);
//				intent.putExtra(WebViewActivity.WIKI_LINK, country.getWikiLink());
//				mActivity.startActivity(intent);
//			}
//		});
//		

		tvName.setText(country.getName());
		tvCapital.setText(country.getCapital());
//		tvRegion.setText(country.getRegion());
		tvContinent.setText(country.getContinent());
		tvContiguous.setText(country.writeContiguousCountries());
//		tvLanguages.setText(country.writeLanguages());
		
//		tvArea.setText(country.getArea() + " km (" + country.getAreaRank() + ")");		
//		tvPopulationTotal.setText(country.getPopulation() + " (" + country.getPopulationRank() + ")");
//		tvPopulationDensity.setText(country.getPopulationDensity() + " ob./km");

		Locale locale = Locale.getDefault();
		String area  = NumberFormat.getNumberInstance(locale).format(country.getArea());
		tvArea.setText(area + " km\u00B2");		
		String population  = NumberFormat.getNumberInstance(locale).format(country.getPopulation());
		tvPopulationTotal.setText(population);
		if(country.getArea() != 0) {
			int density = (int) country.getPopulation() / country.getArea();
			tvPopulationDensity.setText(density + " ob./km\u00B2");
		}
		
		
		
//		tvGovernment.setText(country.getGovernment());
//		tvCurrency.setText(country.getCurrency() == null ? "" : country.getCurrency().toString());
//		
//		tvIso.setText(country.getIsoCode());
//		tvTld.setText(country.getTld());
		try {
			ivFlag.setImageDrawable(GeographyApplication.getInstance().getFlagDrawable(country.getId()));
		} catch (WrongResourceException e) {
			Log.d(CountryDetailPageAdapter.TAG, e.getMessage());
	    }
//		try {
//			ivLoc.setImageDrawable(app.getLocationDrawable(country.getId()));
//		} catch (WrongResourceException e) {
//			Log.d(CountryDetailPageAdapter.TAG, e.getMessage());
//	    }		
		
	}

}
